package com.ccjq.shosestore.web.controller.member;

import com.ccjq.shosestore.member.bo.User;
import com.ccjq.shosestore.member.service.UserService;
import com.ccjq.shosestore.member.service.impl.UserServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "UserServlet")
public class UserServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    private static final String FALG_REG = "reg";//注册界面
    private static final String FALG_REG_SUBMIT = "rs";//提交注册
    private static final String FALG_LOGIN = "login";//登录界面
    private static final String FALG_LOGIN_SUBMIT = "ls";//提交登录
    private static final String FALG_INFO= "info";//用户中心页面
    private static final String FALG_LOGOUT= "logout";//注销

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag = request.getParameter("flag");

        if(StringUtils.isBlank(flag)){//跳转到登录界面
            toLoginPage(request,response);
        }else if(FALG_REG.equals(flag)){//跳转到注册界面
            toRegisterPage(request,response);
        }else if(FALG_REG_SUBMIT.equals(flag)){//执行注册动作
            register(request,response);
        }else if(FALG_INFO.equals(flag)){//跳转到用户中心页面
            toUserInfo(request,response);
        }else if(FALG_LOGOUT.equals(flag)){//用户注销界面
            logout(request,response);
        }else if (FALG_LOGIN.equals(flag)){//跳转到登录页面
           toLoginPage(request,response);
        }else if(FALG_LOGIN_SUBMIT.equals(flag)){//执行登录动作
           login(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doPost(request,response);
    }

    /**
     * 注册动作
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String mobilephone = request.getParameter("phone");
        String password = request.getParameter("password");
        String vicode = request.getParameter("vicode");
        HttpSession session = request.getSession();
        String checkcode =(String)session.getAttribute("checkcode");
        if(checkcode.equalsIgnoreCase(vicode))
        {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setMobilePhone(mobilephone);

            String userId = userService.saveUser(user);
            User user2 = userService.findUserById(userId);
            session.setAttribute("user",user2);
            request.getRequestDispatcher("/WEB-INF/jsp/member/userinfo.jsp").forward(request,response);
            return;
        }else
        {
            request.setAttribute("message","验证码不正确");
            request.getRequestDispatcher("/WEB-INF/jsp/member/register.jsp").forward(request,response);
        }

    }
    private void toRegisterPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/member/register.jsp").forward(request,response);
    }

    /**
     * 登录动作
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String vicode = request.getParameter("vicode");
        HttpSession session = request.getSession();
        String checkcode =(String)session.getAttribute("checkcode");
        if(checkcode.equalsIgnoreCase(vicode))
        {
            User user = userService.findUserByNamePw(username,password);
            session.setAttribute("user",user);
            request.getRequestDispatcher("/WEB-INF/jsp/member/userinfo.jsp").forward(request,response);
            return;
        }else
        {
            request.setAttribute("message","验证码不正确");
            request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
        }

    }

    private void toLoginPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
    }

    /**
     * 注销动作
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.invalidate();
        request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
    }

    /**
     * 任何一个界面点击用户名跳转到登录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void toUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String userId = request.getParameter("userId");
        User user = userService.findUserById(userId);
        session.setAttribute("user",user);
        request.getRequestDispatcher("/WEB-INF/jsp/member/userinfo.jsp").forward(request,response);
    }


}
